﻿Imports System.Text

Public Enum 长度型 As Integer
    字符
    字节
End Enum

Public Class UTILControl
    Public Shared Function CutString(ByVal content As String, _
                                     ByVal maxlength As Integer, _
                                     ByVal 长度类型 As 长度型) As String
        Dim rs As String = content.TrimEnd
        If 长度类型 = 长度型.字符 Then
            If rs.Length > maxlength Then
                rs = rs.Substring(0, maxlength)
            End If
        Else
            Dim byteCount As String = System.Text.Encoding.Default.GetByteCount(content)
            If byteCount > maxlength Then
                Dim count As Int32 = 0
                Dim sb As New System.Text.StringBuilder
                For index As Integer = 0 To content.Length - 1
                    count += Encoding.Default.GetByteCount(rs(index))
                    If count > maxlength Then
                        Exit For
                    End If
                    sb.Append(content(index))
                Next
                rs = sb.ToString
            End If
        End If

        Return rs
    End Function

    Public Shared Function GetStringLength(ByVal content As String, _
                                     ByVal 长度类型 As 长度型) As Integer
        If 长度类型 = 长度型.字符 Then
            Return content.TrimEnd.Length
        Else
            Return System.Text.Encoding.Default.GetByteCount(content.TrimEnd)
        End If
    End Function

End Class
